package com.jyy.prefabricated.controller;

import com.jyy.prefabricated.common.AjaxResult;
import com.jyy.prefabricated.dto.BatchDto;
import com.jyy.prefabricated.entity.ExamCourse;
import com.jyy.prefabricated.entity.ExamCourse;
import com.jyy.prefabricated.exception.CustomException;
import com.jyy.prefabricated.service.ExamCourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;
import java.util.List;

/**
 * 考试关联课程表(ExamCourse)表控制层
 *
 * @author makejava
 * @since 2021-06-01 15:14:01
 */
@Slf4j
@RestController
@RequestMapping("api")
public class ExamCourseController {
    /**
     * 服务对象
     */
    @Resource
    private ExamCourseService examCourseService;
    /**
     * 新增考试关联课程
     *
     * @param ExamCourse
     * @return
     */
    @PostMapping("/ExamCourse")
    public AjaxResult insert(@Valid @RequestBody ExamCourse ExamCourse) {
        ExamCourse service = examCourseService.insert(ExamCourse);
        ExamCourse result = examCourseService.queryById(service.getId());
        return AjaxResult.success(result);
    }

    /**
     * 更新考试关联课程
     *
     * @param ExamCourse
     * @return
     */
    @PutMapping("/ExamCourse")
    public AjaxResult update(@Valid @RequestBody ExamCourse ExamCourse) {

        examCourseService.update(ExamCourse);
        ExamCourse result = examCourseService.queryById(ExamCourse.getId());
        return AjaxResult.success(result);
    }

    /**
     * 删除考试关联课程
     *
     * @param dto
     * @return
     */
    @DeleteMapping("/ExamCourse")
    public AjaxResult update(@Valid @RequestBody BatchDto dto) {
        if (dto.getIds().size() == 0) {
            throw new CustomException("参数id不能为空");
        }
        examCourseService.batchUpdateDeleted(dto);
        return AjaxResult.success();
    }


    /**
     * 查询考试关联课程详情
     *
     * @param id
     * @return
     */
    @GetMapping("/ExamCourse/{id}")
    public AjaxResult selectById(@PathVariable Integer id) {
        log.info("Rest request to find course by id:{}", id);
        ExamCourse result = examCourseService.queryById(id);
        return AjaxResult.success(result);
    }

    /**
     * 查询考试关联课程列表不分页
     *
     * @return
     */
    @GetMapping("/ExamCourse-list")
    public AjaxResult selectByParams(@Valid ExamCourse ExamCourse) {

        List<ExamCourse> result = examCourseService.queryList(ExamCourse);
        return AjaxResult.success(result);
    }

}
